import {
    Scene,
    Vector3,
    MeshBuilder,
    PhysicsAggregate,
    PhysicsShapeType,
    HingeConstraint
} from "@babylonjs/core";

async function createSinglePendulum(scene: Scene, position: Vector3) {
    let box1 = MeshBuilder.CreateBox("hingeBox1", { size: 1 }, scene);
    box1.position.add(new Vector3(0, 1, 0));
    box1.position.add(position);
    box1.scaling.y = 0.2;

    let box2 = MeshBuilder.CreateBox("hingeBox2", { size: 1 }, scene);
    box2.position.add(new Vector3(0, 1, -1));
    box2.position.add(position);
    box2.scaling.y = 0.2;

    let agg1 = new PhysicsAggregate(box1, PhysicsShapeType.BOX, { mass: 0, restitution: 1 }, scene);
    let agg2 = new PhysicsAggregate(box2, PhysicsShapeType.BOX, { mass: 1, restitution: 1 }, scene);

    let joint = new HingeConstraint(
        new Vector3(0, 0, 0),
        new Vector3(0, 0, 1),
        undefined,
        undefined,
        scene
    );
    agg1.body.addConstraint(agg2.body, joint);
}

export async function testHingeConstraint(scene: Scene) {
    createSinglePendulum(scene, new Vector3(0, 0, 0));
}
